﻿using Core.DomainServices;
using Microsoft.Office.Interop.Excel;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.DomainServices
{
    public class Excel2Pdf : IDocumentConversion
    {
        public Microsoft.Office.Interop.Excel.Workbooks excelWorkBook { get; set; }
        private Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application();

        public void ConvertTo(IList<string> files)
        {
            object missing = Type.Missing;
            Workbook workbook = null;
            
                foreach (var item in files)
                {
                    try
                    {
                        excelApplication.Visible = false;
                        excelWorkBook = excelApplication.Workbooks;
                        workbook = excelWorkBook.Open(item);
                        string outputFileName = item.Replace(".xlsx", ".pdf");

                        workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, outputFileName,
                        XlFixedFormatQuality.xlQualityStandard, true, true, Type.Missing, Type.Missing, false, Type.Missing);

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Exception Caught: " + ex.Message);
                    }
                    finally
                    {
                        if (workbook != null)
                        {
                            workbook.Close(XlSaveAction.xlDoNotSaveChanges);
                            while (Marshal.FinalReleaseComObject(workbook) != 0) { };
                            workbook = null;
                        }
                        if (excelWorkBook != null)
                        {
                            excelWorkBook.Close();
                            while (Marshal.FinalReleaseComObject(excelWorkBook) != 0) { };
                            excelWorkBook = null;
                        }
                    }
                }
                if (excelApplication != null)
                {
                    excelApplication.Quit();
                    excelApplication.Application.Quit();
                    while (Marshal.FinalReleaseComObject(excelApplication) != 0) { };
                    excelApplication = null;
                }   
        }

        public void Dispose()
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication);
                Marshal.ReleaseComObject(excelApplication);
                excelApplication = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            catch { }
        }
    }
}
